package com.tian.view.draw.region

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.GestureDetector
import android.view.GestureDetector.SimpleOnGestureListener
import android.view.MotionEvent
import android.view.View
import com.tian.util.view.DisplayUtil

/**
 * @author tiansongtao, Email:tiansongtao@up366.com
 * @date 2022/12/12 18:09
 */
class RegionView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
        View(context, attrs, defStyleAttr) {

    private val TAG = "RegionView"

    private val mPaint: Paint = Paint()

    private var gestureDetector: GestureDetector


    /**
     *  region1.op(region2, op) Region.Op 解释
     * [Region.Op.DIFFERENCE]  补集：region1-(region1与region2交集)
     * [Region.Op.INTERSECT]   交集：region1与region2交集
     * [Region.Op.UNION]       并集：region1与region2并集
     * [Region.Op.XOR]         异并集：(region1与region2并集)-(region1与region2交集)
     * [Region.Op.REVERSE_DIFFERENCE] 反转补集：region2-(region1与region2交集)
     * [Region.Op.REPLACE]     替换：只显示region2区域
     */
    var opIndex = 0
    var op = Region.Op.DIFFERENCE
        set(value) {
            field = value
            postInvalidate()
        }
    val msgs = arrayOf("补集：region1-(region1与region2交集)",
            "交集：region1与region2交集", "并集：region1与region2并集", "异并集：(region1与region2并集)-(region1与region2交集)",
            "反转补集：region2-(region1与region2交集)", "替换：只显示region2区域")


    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context) : this(context, null)

    init {
        mPaint.color = Color.GREEN
        mPaint.strokeWidth = DisplayUtil.dpToPx(4f)
        mPaint.style = Paint.Style.STROKE
        mPaint.textSize = DisplayUtil.spToPx(14f)
        gestureDetector = GestureDetector(context, object : SimpleOnGestureListener() {
            override fun onDown(e: MotionEvent?) = true
            override fun onSingleTapConfirmed(e: MotionEvent?): Boolean {
                opIndex++
                if (opIndex > 5) {
                    opIndex = 0
                }
                op = when (opIndex) {
                    0 -> Region.Op.DIFFERENCE
                    1 -> Region.Op.INTERSECT
                    2 -> Region.Op.UNION
                    3 -> Region.Op.XOR
                    4 -> Region.Op.REVERSE_DIFFERENCE
                    else -> Region.Op.REVERSE_DIFFERENCE
                }
                return true
            }
        })
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)


        val path1 = Path()
        val path2 = Path()
        path1.addCircle(500f, 600f, 300f, Path.Direction.CW)
        path2.addCircle(500f, 600f + 300f, 300f, Path.Direction.CW)

        mPaint.style = Paint.Style.STROKE
        mPaint.color = Color.GREEN
        canvas.drawPath(path1, mPaint)
        canvas.drawPath(path2, mPaint)

        mPaint.color = Color.RED
        mPaint.style = Paint.Style.FILL
        mPaint.textAlign = Paint.Align.CENTER
        val pathMeasure = PathMeasure(path1, false)
        canvas.drawTextOnPath("点击切换Region.Op", path1, pathMeasure.length / 4, -20f, mPaint)

        val region1 = Region()
        val region2 = Region()
        region1.setPath(path1, Region(0, 0, 1500, 1500))
        region2.setPath(path2, Region(0, 0, 1500, 1500))

        region1.op(region2, op)

        val iter = RegionIterator(region1)
        var rect = Rect()
        while (iter.next(rect)) {
            canvas.drawRect(rect, mPaint)
        }
        mPaint.textAlign = Paint.Align.CENTER
        canvas.drawText("使用Op=${op.name}", measuredWidth.toFloat() / 2, 60f, mPaint)
        canvas.drawText(msgs[opIndex], measuredWidth.toFloat() / 2, 120f, mPaint)


    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        return gestureDetector.onTouchEvent(event)
    }
}